class Solution(object):
    def longestCommonSubsequence(self, arrays):
        m = len(arrays)
        index = [0] * m
        k = 0
        c = 0
        ans = []
        while True:
            if c == 0:
                if index[k % m] < len(arrays[k % m]):
                    c = 1
                    base = arrays[k % m][index[k % m]]
                    index[k % m] += 1
                else:
                    break
            else:
                while index[k % m] < len(arrays[k % m]) and arrays[k % m][index[k % m]] < base:
                    index[k % m] += 1
                if index[k % m] == len(arrays[k % m]):
                    break
                else:
                    if arrays[k % m][index[k % m]] == base:
                        c += 1
                        if c == m:
                            ans.append(base)
                            c = 0
                    else:
                        base = arrays[k % m][index[k % m]]
                        c = 1
                    index[k % m] += 1
            k += 1
        return ans



